package com.home.cca.network;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.home.cca.model.Goods;
import com.home.cca.model.Mat;
import com.home.cca.model.Result;

import java.io.IOException;
import java.util.List;
import java.util.Objects;

import okhttp3.FormBody;
import okhttp3.Request;
import okhttp3.RequestBody;

public class UpdateGoodsRequest {

    private static final String URL = ApiClient.getBaseUrl() + "/SupplierManager/GoodsMgr/CreateSupMat";

    public static void update(Goods goods) throws IOException {
        Request request = new Request.Builder()
                .url(URL)
                .post(createParams(goods))
                .header("Accept", "*/*")
                .header("Accept-Language", "zh-CN,zh;q=0.9")
                .header("Connection", "keep-alive")
                .header("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8")
                .header("Cookie", ApiClient.getCookie())
                .header("Origin", ApiClient.getBaseUrl())
                .header("Referer", ApiClient.getBaseUrl())
                .header("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/129.0.0.0 Safari/537.36")
                .header("X-Requested-With", "XMLHttpRequest")
                .header("docktoken", "")
                .header("key", "15930935383")
                .build();

        String execute = ApiClient.execute(request);
        Result<?> res = new Gson().fromJson(execute, Result.class);
        if (!res.isSuccess()) {
            throw new IOException(res.getMessage());
        }

    }

    /**
     * 创建并返回包含所有需要参数的 RequestBody。
     */
    private static RequestBody createParams(Goods goods) throws IOException {
        return new FormBody.Builder()
                .add("MatId", String.valueOf(goods.getMatId()))
                .add("MatName", String.valueOf(goods.getName()))
                .add("NutriUnit", String.valueOf(goods.getNutriUnit()))
                .add("ScanBarCode", String.valueOf(goods.getScanBarCode()))
                .add("StorageQty", String.valueOf(goods.getStorageQty()))
                .add("Price", String.valueOf(goods.getPrice()))
                .add("BarCode", "")
                .add("AICode", "")
                .add("LocationCode", "")
                .add("Unit", String.valueOf(goods.getUnit()))
                .add("Standard", "")
                .add("Uuid", "")
                .add("MatExpiration", String.valueOf(goods.getMatExpiration()))
                .add("SupMatClsId", getSupMatClsId(goods))
                .add("UnWeigh", "false")
                .add("Structure", "0")
                .add("Brand", "")
                .add("MatModel", "")
                .add("ProdAddress", "")
                .add("ProdFactory", "")
                .add("ProdCPhoModele", "")
                .add("ProdPkgType", "")
                .add("Des", "")
                .add("StoreType", "1")
                .add("MatPics[]", getMatPicJson(goods))
                .add("MatClassId", "0")
                .add("SupplyScope", "0")
                .add("IsAdditive", "false")
                .add("MaximumUsage", "0")
                .add("Function", "")
                .add("LowConsumables", "false")
                .add("IsRound", "false")
                .add("Strategy", "0")
                .add("DefaultQty", "1")
                .build();
    }

    private static String getMatPicJson(Goods goods) {
        List<String> list = new Gson().fromJson(goods.getMatPicJson(), new TypeToken<List<String>>() {
        }.getType());
        return String.join(",", list);
    }

    private static String getSupMatClsId(Goods goods) throws IOException {
        List<Mat> mats = QueryMatRequest.query();
        for (Mat mat : mats) {
            if (Objects.equals(mat.getName().trim(), goods.getMatClassName().trim())) {
                return String.valueOf(mat.getId());
            }
        }
        return "";
    }
}
